import os
import time
from time import sleep
import io
import sys

class Command:
    #定义一些属性
    width = 10
    height = 20
    chooseFunc = ''

    #初始化模拟触屏指令
    def __init__(self, chooseFunc):
        self.phoneSize()
        self.chooseFunc = chooseFunc


    def adb_connect1(self, IP):
        os.system(f"adb connect {IP}")

    #adb_shell终端指令输入
    def adb_cmd(self, cmd):
        # 创建一个内存中的对象，用于存储 print 的输出
        #output = io.StringIO()
        # 重定向标准输出到内存对象
        #sys.stdout = output
        output_data= os.popen(f"adb shell {cmd}")

        return f"{output_data.read()}"
        #print(output_date.read())


    #获取屏幕物理尺寸
    def phoneSize(self):
        ssize = str(self.adb_cmd(f'wm size'))
        print(ssize)
        sizeStr =ssize.split(":")[-1].strip()
        self.width, self.height = map(int, sizeStr.split("x"))
        

    #模拟鼠标的点击事件，x，y为点击的坐标
    def touch(self,x,y):
        
        #sleep(delay_t)
        self.adb_cmd(f'input tap {x} {y}')
        #sleep(0.02)
        #adb_cmd(f'input tap {tx2} {ty2}')

    #模拟文字输入事件，text输入字符串
    def text(self, text):
        self.adb_cmd(f'input text {text}')

    #模拟滑动事件，x1,y1为起始坐标,x2,y2为终点坐标，t为滑动的时间
    def swipe_num(self, t = 100):
        sx1 = self.width / 2 
        sy1 = self.height * 2 / 3
        sx2 = self.width / 2
        sy2 = self.height * 1 / 3
            #t = 1000*t
        #print(self.width, ' ')
        #print(sx1,' ' ,sy1)
        self.adb_cmd(f'input touchscreen swipe {sx1} {sy1} {sx2} {sy2} {t}')
        #sleep(delay_t)
            
    #模拟按键事件，3代表回主页按键，4代表返回按键
    def keyevent(self, x):
        self.adb_cmd(f'input keyevent {x}')

    #模拟拖动事件，x1,y1为起始坐标,x2,y2为终点坐标，t为拖动的时间
    def draganddrop(self, x1,y1,x2,y2,t):
        self.adb_cmd(f'input draganddrop {x1} {y1} {x2} {y2} {t}')
    
    
    def advert(self, tx1, ty1, tx2, ty2, delay_t = 10):
        for number in range(5):
            sleep(delay_t)
            self.touch(tx1,ty1)
            sleep(0.02)
            self.touch(tx2,ty2)

    def vedio(self):
        while(1):
            self.swipe_num()
            sleep(10)
    print('please:')

if __name__ == "__main__":
    print('please:')
    chooseFunction = input("请输入你的模拟操作,swipe or advert \n")
    c = Command(chooseFunction)
    if c.chooseFunc == 'swipe':
        c.vedio()
    elif c.chooseFunc == 'advert':
        c.advert(1000,130,540,1340)
    

    
